package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Actgx;
import com.qianniu.napi.admin.service.IActgxService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 行为关系表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-11-26
 */
@Controller
public class ActgxController {

    private static final Logger logger = LoggerFactory.getLogger(ActgxController.class);

    @Autowired
    private IActgxService actgxService;

    @RequestMapping("admin/actgx/index")
    public String index() {
        return "admin/actgx";
    }

    @RequestMapping("/admin/actgx/list")
    @ResponseBody
    public Page<Actgx> list(@RequestParam(value = "offset", required = false) Integer offset,
                            @RequestParam(value = "status", required = false) Integer status,
                            @RequestParam(value = "search", required = false) String search,
                            @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Actgx> wrapper = new EntityWrapper<Actgx>();
        wrapper.eq("mid", ObjectUtil.getMid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("id", false);
        Page<Actgx> page = actgxService.selectPage(new Page<Actgx>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/actgx/add")
    public String add(@Valid Actgx bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/actgx";
    }

    @RequestMapping("/admin/actgx/getActgxById")
    @ResponseBody
    public Actgx getActgxById(long id) {
        Actgx actgx = actgxService.selectById(id);
        return actgx;
    }

    @RequestMapping("/admin/actgx/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = actgxService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/actgx/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Actgx bean = new Actgx();
        bean.setId(id);
//        bean.setState(state);
//        bean.setModifytime(new Date());
//        bean.setModifier(ObjectUtil.getUid());
        actgxService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/actgx/modify")
    public String update(Actgx bean) {
//        bean.setModifytime(new Date());
//        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = actgxService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }
}
